import {hrAjax as proFetch} from 'src/hrpub/common/utils/utils';
import {toast} from 'nc-lightapp-front'
import saveLine from "./saveLine";

export default function checkSaveLine() {
    const {areaCode} = this.state;
    const {selectedPsn, orgVal, pageid, editTable, modal, pagecode} = this.props;
    const changedRows = editTable.getChangedRows(areaCode);

    if (!changedRows.length) {
        this.cancelEdit();
        return;
    }
    const allRows = editTable.getAllRows(areaCode);
    let checkRows = allRows.map(row => {
        if (row.status === '0') {
            return {
                status: '3',
                values: {}
            }
        } else {
            return row
        }
    });
    const requireFlag = editTable.checkRequired(areaCode, checkRows);
    if (!requireFlag) return;
    const index = allRows.findIndex(item => item.rowid === changedRows[0].rowid);
    let nextRow = null, preRow = null;
    if (index !== 0) preRow = {areacode: areaCode, rows: [allRows[index - 1]]};
    if (index !== allRows.length - 1) nextRow = {areacode: areaCode, rows: [allRows[index + 1]]};
    let postData = {
        areaCode,
        editType: this.editType,
        pk_org: orgVal.refpk,
        pk_psnjob: selectedPsn,
        curRow: {areacode: areaCode, rows: changedRows},
        nextRow,
        preRow
    };
    const callback = () => {
        proFetch({
            url: '/nccloud/hrtrn/pub/RecordsSaveCheckAction.do',
            body: postData,
        })
            .then((res) => {
                if (res.success) {
                    const {confirmMsg, warningMsg} = res.data;
                    this.alertModalNode.show(confirmMsg, warningMsg, () => {
                        modal.show('isSynWork');
                        //saveLine.call(this)
                    })
                }
            });
    };

    const cardData = {
        pageid: pagecode,
        gridModel: {
            areacode: areaCode,
            pageinfo: {},
            rows: changedRows
        }
    };
    this.props.validateToSave(cardData, callback, {[areaCode]: 'editTable'}, 'grid');
}
